package DepreciationInfo

import (
	"ToolLibray/jwtx"
	"context"
	"property_api/internal/svc"
	"property_api/internal/types"
	"property_api/utils/result"
	"property_api/utils/xform"
	"property_service/client/asset_service"

	"github.com/zeromicro/go-zero/core/logx"
)

type EditLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewEditLogic(ctx context.Context, svcCtx *svc.ServiceContext) *EditLogic {
	return &EditLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

// Edit 修改折旧信息的逻辑
func (l *EditLogic) Edit(req *types.DepreciationInfoEdit) (resp *types.Response, err error) {
	// 设置租户ID，从上下文中获取
	tenantID := jwtx.GetUserTenantId64(l.ctx)
	// 设置更新用户，从上下文中获取
	userIdStr := xform.Int64ToString(jwtx.GetUserId(l.ctx))

	// 调用服务层方法，修改折旧信息记录
	// 服务层方法 DepreciationInfoEdit 会处理实际的数据库更新操作
	res, err := l.svcCtx.AssetService.DepreciationInfoEdit(l.ctx, &asset_service.DepreciationInfo{
		ID:                           req.ID,                           // 记录ID
		TenantID:                     tenantID,                         // 租户ID
		AssetID:                      req.AssetID,                      // 资产ID（关联资产表）
		StartDate:                    req.StartDate,                    // 开始折旧日期
		AssetValue:                   req.AssetValue,                   //资产原值
		NetValue:                     req.NetValue,                     // 当前净值
		UsefulLife:                   req.UsefulLife,                   // 预计使用年限
		DepreciatedMonths:            req.DepreciatedMonths,            // 已折旧月份
		DepreciationRate:             req.DepreciationRate,             //月折旧率
		DepreciationRateYear:         req.DepreciationRateYear,         //年折旧率
		StatusID:                     req.StatusID,                     // 状态ID(关联状态表)
		Method:                       req.Method,                       // 折旧方式（平均年限法等）
		Remark:                       req.Remark,                       // 折旧备注
		UpdatedAtuser:                userIdStr,                        // 更新用户
		NetResidueValue:              req.NetResidueValue,              //净残值
		AccumulatedDepreciationValue: req.AccumulatedDepreciationValue, //累计折旧额
		NetWorthValue:                req.NetWorthValue,                //净值
		NeteValue:                    req.NeteValue,                    //净额
		CurrencyId:                   req.CurrencyId,                   //币种ID(关联币种表)
		CurrencyName:                 req.CurrencyName,                 //币种名称
		ExpectedUsefulLife:           req.ExpectedUsefulLife,           //预计使用年限（月）
		NetResidueRate:               req.NetResidueRate,               //净残值率
	})

	// 如果服务层返回错误，返回统一的错误响应
	if err != nil {
		return result.Err(err).End()
	}

	// 返回成功的响应
	return result.Suc(res).End()
}
